package cn.sciento.wechat.enterprise.service.impl;

import com.alibaba.fastjson.JSON;
import javax.annotation.Resource;
import cn.sciento.wechat.enterprise.dto.AgentDTO;
import cn.sciento.wechat.enterprise.dto.AgentListDTO;
import cn.sciento.wechat.enterprise.dto.DefaultResultDTO;
import cn.sciento.wechat.enterprise.dto.MenuDTO;
import cn.sciento.wechat.enterprise.dto.SetAgentDTO;
import cn.sciento.wechat.enterprise.service.WechatCorpAgentManageService;
import org.springframework.http.HttpEntity;
import org.springframework.web.client.RestTemplate;

public class WechatCorpAgentManageServiceImpl implements WechatCorpAgentManageService {
  @Resource(name = "wdRestTemplate")
  protected RestTemplate restTemplate;
  
  public AgentDTO getAgentByID(String agentId, String accessToken) {
    return (AgentDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/agent/get?access_token=" + accessToken + "&agentid=" + agentId, AgentDTO.class);
  }
  
  public AgentListDTO getAgentList(String accessToken) {
    return (AgentListDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/agent/list?access_token=" + accessToken, AgentListDTO.class);
  }
  
  public DefaultResultDTO setAgent(SetAgentDTO setAgentDTO, String accessToken) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/agent/set?access_token=" + accessToken, new HttpEntity(JSON.toJSONString(setAgentDTO)), DefaultResultDTO.class);
  }
  
  public DefaultResultDTO createMenu(String accessToken, String agentid, MenuDTO menuDTO) {
    return (DefaultResultDTO)this.restTemplate.postForObject("https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken + "&agentid" + agentid, new HttpEntity(JSON.toJSONString(menuDTO)), DefaultResultDTO.class);
  }
  
  public MenuDTO getMenu(String accessToken, String agentid) {
    return (MenuDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken + "&agentid" + agentid, MenuDTO.class);
  }
  
  public DefaultResultDTO deleteMenu(String accessToken, String agentid) {
    return (DefaultResultDTO)this.restTemplate.getForObject("https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken + "&agentid" + agentid, DefaultResultDTO.class);
  }
}
